<template>
	<view class="preview" v-if="currentIndex>=0">
		<swiper circular :current="currentIndex" @change="swiperChange">
			<swiper-item v-for="(item,index) in classList" :key="item._id">
				<image @click="maskChange" :src="item.picurl" mode="aspectFill" v-if="readImgs.includes(index)"></image>
			</swiper-item>
		</swiper>
		<view class="mask" v-if="isMask">
			<view @click="onBack" class="goBack" :style="{top:(getStatusBarHeight() || 15)+'px'}">
				<uni-icons type="back" color="#fff" size="20"></uni-icons>
			</view>
			<view class="count">{{currentIndex+1}} / {{classList.length}}</view>
			<view class="time">
				<uni-dateformat :date="new Date()" format="hh:mm"></uni-dateformat>
			</view>
			<view class="date">
				<uni-dateformat :date="new Date()" format="MM月dd日"></uni-dateformat>
			</view>
			<view class="footer">
				<view class="box" @click="onInfo">
					<uni-icons type="info" size="28"></uni-icons>
					<view class="text">信息</view>
				</view>
				<view class="box" @click="onScore">
					<uni-icons type="star" size="28"></uni-icons>
					<view class="text">{{classList[currentIndex].score}}</view>
				</view>
				<view class="box" @click="onDownload">
					<uni-icons type="download" size="28"></uni-icons>
					<view class="text">下载</view>
				</view>
			</view>
		</view>
		<uni-popup ref="infoPopup" type="bottom" :safe-area="false">
			<view class="infoPopup">
				<view class="popheader">
					<view></view>
					<view class="title">
						壁纸信息
					</view>
					<view class="close" @click="onCloseInfo">
						<uni-icons type="closeempty" size="18" color="#999"></uni-icons>
					</view>
				</view>
				<scroll-view scroll-y>
					<view class="content">
						<view class="row">
							<view class="label">图片ID:</view>
							<text class="value" selectable>{{classList[currentIndex]._id}}</text>
						</view>
						<view class="row">
							<view class="label">分类:</view>
							<text class="value class" selectable>{{classify}}</text>
						</view>
						<view class="row">
							<view class="label">发布者:</view>
							<text class="value" selectable>{{classList[currentIndex].nickname}}</text>
						</view>
						<view class="row">
							<view class="label">评分:</view>
							<view class="value roteBox">
								<uni-rate :value="classList[currentIndex].score" readonly touchable />
								<text class="score">{{classList[currentIndex].score}} 分</text>
							</view>
						</view>
						<view class="row">
							<view class="label">摘要:</view>
							<text class="value" selectable>{{classList[currentIndex].description}}</text>
						</view>
						<view class="row">
							<view class="label">标签:</view>
							<view class="value tabs">
								<view class="tab" v-for="item in classList[currentIndex].tabs" :key="item">{{item}}</view>
							</view>
						</view>
						<view class="copyright">
							声明：本图片来自用户投稿，非商业使用，用于免费学习交流，如侵犯了您的权益，您可以拷贝壁纸ID举报至平台，邮箱lakdlaskl@163.com，管理员将删除侵权壁纸，维护你的权益。
						</view>
					</view>
				</scroll-view>
				<view class="safe-area-inset-bottom"></view>
			</view>
		</uni-popup>
		<uni-popup ref="scorePopup" type="center" :is-mask-click="false">
			<view class="infoPopup scorePopup">
				<view class="popheader">
					<view></view>
					<view class="title">壁纸评分</view>
					<view class="close" @click="onCloseScore">
						<uni-icons type="closeempty" size="18" color="#999"></uni-icons>
					</view>
				</view>
				<view class="content">
					<uni-rate v-model="score" allowHalf :readonly="isScored"></uni-rate>
					<text class="text">{{score}} 分</text>
				</view>

				<view class="footer">
					<button size="mini" :disabled="!score||isScored" plain
						@click="onScoreSubmit">{{isScored?'已评分':'确认评分'}}</button>
				</view>

			</view>
		</uni-popup>
	</view>
</template>

<script setup>
	import {
		ref,
		watch
	} from 'vue';
	import {
		getStatusBarHeight
	} from '@/utils/system';
	import {
		onLoad,
		onShareAppMessage,
		onShareTimeline
	} from '@dcloudio/uni-app'
	import {
		getDownloadWallApi,
		getSetupScoreApi
	} from '../../api/apis';

	const classList = ref([])
	const storageClassList = uni.getStorageSync('classList')
	classList.value = storageClassList.map(item => {
		return {
			...item,
			picurl: item.smallPicurl.replace('_small.webp', '.jpg')
		}
	})

	const readImgs = ref([])
	const currentIndex = ref(-1)

	watch(currentIndex, () => {
		score.value = classList.value[currentIndex.value].userScore || 0
		isScored.value = !!score.value
	})

	onLoad((e) => {
		currentIndex.value = classList.value.findIndex(item => item._id === e.id)
		arrAdd(currentIndex.value)
	})

	const swiperChange = (e) => {
		currentIndex.value = e.detail.current
		arrAdd(currentIndex.value)
	}
	const arrAdd = (current) => {
		const data = [
			current === 0 ? classList.value.length - 1 : current - 1,
			current,
			current === classList.value.length - 1 ? 0 : current + 1
		]
		data.forEach(item => {
			if (!readImgs.value.includes(item)) readImgs.value.push(item)
		})
	}

	const isMask = ref(true)
	const maskChange = () => {
		isMask.value = !isMask.value
	}

	const classify = uni.getStorageSync('classify')
	const infoPopup = ref(null)
	const onInfo = () => {
		infoPopup.value.open()
	}

	const onCloseInfo = () => {
		infoPopup.value.close()
	}

	const score = ref(0)
	const isScored = ref(false)
	const scorePopup = ref(null)
	const onScore = () => {
		scorePopup.value.open()
	}
	const onCloseScore = () => {
		score.value = classList.value[currentIndex.value].userScore || 0
		scorePopup.value.close()
	}
	const onScoreSubmit = async () => {
		if (!isScored.value) {
			const res = await getSetupScoreApi({
				classid: classList.value[currentIndex.value].classid,
				wallId: classList.value[currentIndex.value]._id,
				userScore: score.value
			})
			classList.value[currentIndex.value] = {
				...classList.value[currentIndex.value],
				userScore: score.value
			}
			isScored.value = true
			uni.setStorageSync('classList', classList.value)
			uni.showToast({
				title: '评分成功'
			})
		}
		scorePopup.value.close()
	}

	const onBack = () => {
		uni.navigateBack()
	}

	const onDownload = () => {
		// #ifdef H5
		uni.showModal({
			content: '请长按后选图片另存为...',
			showCancel: false,
			success: async (res) => {
				if (res.confirm) {
					const res1 = await getDownloadWallApi({
						classid: classList.value[currentIndex.value].classid,
						wallId: classList.value[currentIndex.value]._id
					})
				}
			}
		})
		// #endif
		// #ifndef H5
		uni.getImageInfo({
			src: classList.value[currentIndex.value].picurl,
			success: (res) => {
				uni.saveImageToPhotosAlbum({
					filePath: res.path,
					success: async (res1) => {
						const res2 = await getDownloadWallApi({
							classid: classList.value[currentIndex.value].classid,
							wallId: classList.value[currentIndex.value]._id
						})
						console.log(res2);
					},
					fail: (err) => {
						if (err.errMsg === 'saveImageToPhotosAlbum:fail cancel') {
							uni.showToast({
								title: '保存失败，请重新下载',
								icon: 'none'
							})
							return
						}
						uni.showModal({
							title: '授权提示',
							content: '需要开启相册权限',
							success: (res) => {
								if (res.confirm) {
									uni.openSetting({
										success: (setting) => {
											if (setting.authSetting['scope.writePhotosAlbum']) {
												uni.showToast({
													title: '获取授权成功',
													icon: 'none'
												})
											} else {
												uni.showToast({
													title: '获取授权失败',
													icon: 'none'
												})
											}
										}
									})
								}
							}
						})
					}
				})
			},
			fail: (err) => {
				console.log(err);
			}
		})
		// #endif
	}

	// 分享给好友
	onShareAppMessage(e => {
		return {
			title: "壁纸",
			path: '/pages/index/index'
		}
	})

	// 分享到朋友圈
	onShareTimeline(() => {
		return {
			title: '壁纸~~~',

		}
	})
</script>

<style lang="scss" scoped>
	.preview {
		width: 100%;
		height: 100vh;
		position: relative;

		swiper {
			width: 100%;
			height: 100%;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.mask {
			&>view {
				position: absolute;
				left: 0;
				right: 0;
				margin: auto;
				width: fit-content;
				color: white;
			}

			.goBack {
				width: 38px;
				height: 38px;
				background: rgba(0, 0, 0, 0.5);
				left: 30rpx;
				margin-left: 0;
				border-radius: 100px;
				top: 0;
				backdrop-filter: blur(10rpx);
				border: 1px solid rgba(255, 255, 255, 0.3);
				display: flex;
				justify-content: center;
				align-items: center;
			}

			.count {
				top: 10vh;
				background: rgba(0, 0, 0, 0.3);
				font-size: 28rpx;
				border-radius: 40rpx;
				padding: 8rpx 28rpx;
				backdrop-filter: blur(10rpx);
			}

			.time {
				font-size: 140rpx;
				top: calc(10vh + 80rpx);
				font-weight: 100;
				line-height: 1em;
				text-shadow: 0 4rpx rgba(0, 0, 0, 0.3);
			}

			.date {
				font-size: 34rpx;
				top: calc(10vh + 230rpx);
				text-shadow: 0 4rpx rgba(0, 0, 0, 0.3);
			}

			.footer {
				background: rgba(255, 255, 255, 0.8);
				bottom: 10vh;
				width: 65vw;
				height: 120rpx;
				border-radius: 120rpx;
				display: flex;
				flex-direction: row;
				justify-content: space-around;
				align-items: center;
				color: #000;
				box-shadow: 0 2rpx 0 rgba(0, 0, 0, 0.1);
				backdrop-filter: blur(20rpx);

				.box {
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					align-items: center;
					padding: 2rpx 12rpx;
					height: 100rpx;

					.text {
						font-size: 26rpx;
						color: $text-font-color-2;
					}
				}
			}
		}

		.infoPopup {
			background: #fff;
			padding: 30rpx;
			border-radius: 30rpx 30rpx 0 0;
			overflow: hidden;

			.popheader {
				display: flex;
				flex-direction: row;
				justify-content: space-between;
				align-items: center;

				.title {
					color: $text-font-color-2;
					font-size: 26rpx
				}

				.close {
					padding: 10rpx;
				}
			}

			scroll-view {
				max-height: 60vh;

				.content {
					.row {
						display: flex;
						padding: 16rpx 0;
						font-size: 32rpx;
						line-height: 1.7em;

						.label {
							color: $text-font-color-3;
							width: 150rpx;
							text-align: right;
							font-size: 30rpx;

						}

						.value {
							flex: 1;
							width: 0;
							margin-left: 20rpx;
							font-size: 30rpx
						}

						.class {
							color: $brand-theme-color;
						}

						.roteBox {
							display: flex;
							align-items: center;

							.score {
								margin-left: 20rpx;
								font-size: 26rpx;
								color: $text-font-color-2;
							}
						}

						.tabs {
							display: flex;
							flex-wrap: wrap;
							align-items: center;

							.tab {
								border: 1px solid $brand-theme-color;
								color: $brand-theme-color;
								font-size: 22rpx;
								padding: 10rpx 30rpx;
								border-radius: 40rpx;
								line-height: 24rpx;
								margin: 0 10rpx 10rpx 0;
							}
						}
					}

					.copyright {
						font-size: 28rpx;
						padding: 20rpx;
						background: #f6f6f6;
						color: #666;
						border-radius: 10rpx;
						margin: 20rpx 0;
						line-height: 1.6em;
					}
				}
			}
		}

		.scorePopup {
			width: 70vw;
			border-radius: 30rpx;

			.content {
				display: flex;
				justify-content: center;
				align-items: center;
				padding: 30rpx 0;

				.text {
					color: #ffca3e;
					padding-left: 10rpx;
					line-height: 1em;
				}
			}

			.footer {
				display: flex;
				justify-content: center;
				margin: 50rpx 0;
			}
		}
	}
</style>